<?php
namespace app\wechat\controller;
class Home extends Applet {
    protected $totalCounts=[
        'A'=>'严重违法',
        'B'=>'逾期风险',
        'C'=>'诉讼风险',
        'D'=>'潜在风险',
        'E'=>'多头贷款',
    ];
    protected $list=[
        'A'=>'严重违法',
        'A02'=>'名下公司税务重大违法',
        'B'=>'逾期风险',
        'B01'=>'失联',
        'B02'=>'贷款不良',
        'B03'=>'短时逾期',
        'B04'=>'逾期',
        'C'=>'诉讼风险',
        'C01'=>'失信被执行人',
        'C02'=>'被执行人',
        'C03'=>'裁判文书',
        'D'=>'潜在风险',
        'D01'=>'疑似催收风险',
        'D02'=>'名下公司存在违规行为',
        'D03'=>'来自信贷高风险区域 ',
        'D04'=>'其他潜在风险',
        'E'=>'多头借贷',
        'E01'=>'7天内贷款数',
        'E02'=>'一个月内贷款数',
        'E03'=>'三个月内贷款数',
    ];

   /**
	* 首页
	*/
    public function index(){

        $data = input('post.');
        $this -> checkopenid($data);

        //轮播
        $carousel = db('carousel') -> where(array('uid'=>2,'type'=>'mall')) -> find();
        $carousel['carousel'] = serializeMysql($carousel['carousel'],1);
        foreach($carousel['carousel'] as $k=>$v){
            if($v['imgpath']){
                $swiper[] = changeurl($v['imgpath']);
            }
        }
        $rdata = array(
            'carousel'		=> $swiper,
        );
        return json($rdata);
    }
    /**
     * 提交页
     */
    public function order(){
        $data = input('post.');

        $config=db('shuju_config')->where(array('uid'=>2))->find();
        $vip=db(\tname::vip)->where(array('openid'=>$data['openid']))->find();
        $rdata = array(
            'content'		=> $config['content'],
            'free'          => $vip['free']
        );
        return json($rdata);
    }
    /**
     * 分享
     */
    public function share(){
        $data = input('post.');

        $config=db('shuju_config')->where(array('uid'=>2))->find();
        $vip=db(\tname::vip)->where(array('openid'=>$data['openid']))->find();
        if($vip['is_free']==0){
            db(\tname::vip)->where(array('openid'=>$data['openid']))->setInc("free",$config['num']);
            db(\tname::vip)->where(array('openid'=>$data['openid']))->update(array("is_free"=>1));
        }
    }
    /**
     * 提交表单
     */
    public function submit(){
        $data = input('post.');

        $card=$this->is_idcard($data['people']);
        if(!$card){
            return json(array('ret'=>0,'msg' => '请填写正确的身份证号'));
        }
        if(!check_mobilephone($data['phone'])){
            return json(array('ret'=>0,'msg' => '请填写正确的手机号'));
        }
        $vip=db(\tname::vip)->where(array('openid'=>$data['openid']))->find();
        $shuju=db('shuju_config')->where(array('uid'=>2))->find();
        $ordernumber=date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 4);
        //支付
        $infodata = array(
            'uid'			=> 2,
            'openid'		=> $data['openid'],
            'nickname'     => $vip['nickname'],
            'headimgurl'   => $vip['headimgurl'],
            'ordernumber'  => $ordernumber,
            'name'		    => $data['name'],
            'phone'		    => $data['phone'],
            'people'		=> $data['people'],
            'price'		    => $shuju['price'],
            'createtime'	=> time()
        );
        $res = db('shuju_order') -> insert($infodata);
        if($res){
            //判断是否有免费次数
            if($vip['free']>=1){
                $orderinfo = db('shuju_order') -> where(array('ordernumber'=>$ordernumber)) -> find();
                if($orderinfo['ispay'] != 1){
                    //修改订单数据
                    $orderdata=array(
                        'ispay'  => 1,
                    );
                    //访问接口
//                    $result1=phone($orderinfo['name'],$orderinfo['phone'],$orderinfo['people']);//手机认证
//                    $orderdata['returndata']=serializeMysql($result1);
                    $result2=black($orderinfo['name'],$orderinfo['phone'],$orderinfo['people']);//黑名单认证
                    if(array_key_exists('data',$result2['result'])){
                        $totalCounts = $result2['result']['data']['totalCounts'];
                        foreach($totalCounts as $key=>&$value){
                            $value['blackType'] = $this->totalCounts[$value['blackType']];
                            $ccc['a'][$value['blackType']]=$value;
                        }
                        $list = $result2['result']['data']['list'];
                        $lists['b']=arr()['b'];
                        foreach($list as $key=>&$value){
                            $value['blackRiskType'] = $this->list[$value['blackRiskType']];
                            $value['blackFactsType'] = $this->list[$value['blackFactsType']];
                            if($value['blackRiskType']!="多头借贷"){
                                $ccc['b'][$value['blackRiskType']]['a']=$value['blackRiskType'];
                                $ccc['b'][$value['blackRiskType']]['b'][]=$value;
//                                $lists['b'][$value['blackRiskType']]['b']=array_merge(arr()['b'][$value['blackRiskType']]['b'],$ccc['b'][$value['blackRiskType']]['b']);
                                $lists['b'][$value['blackRiskType']]['b']=$ccc['b'][$value['blackRiskType']]['b'];
                            }else{
//                                $ccc['b'][$value['blackFactsType']][]=$value;
                                $ccc['b'][$value['blackFactsType']]['a']=$value['blackFactsType'];
                                $ccc['b'][$value['blackFactsType']]['b'][]=$value;
//                                $lists['b'][$value['blackFactsType']]['b']=array_merge(arr()['b'][$value['blackFactsType']]['b'],$ccc['b'][$value['blackFactsType']]['b']);
                                $lists['b'][$value['blackFactsType']]['b']=$ccc['b'][$value['blackFactsType']]['b'];
                            }

                        }
                        $lists['a']=array_merge(arr()['a'],$ccc['a']);
//                        $list['b']=array_merge(arr()['b'],$ccc['b']);
                       $result2['result']['data'] =  $lists ;
                    }else{
                        $result2['result']['data']= arr();
                    }

                    $orderdata['return']=serializeMysql($result2);
                    $re=db('shuju_order') -> where(array('id'=>$orderinfo['id'])) -> update($orderdata);
                    db(\tname::vip)->where(array('openid'=>$data['openid']))->setDec("free");
                    if($re){
                        return json(array('ret'=>1,'id'=>$orderinfo['id'],'free'=>1));
                    }else{
                        return json(array('ret'=>0));
                    }
                }
            }else{
                $orderinfo = db('shuju_order') -> where(array('ordernumber'=>$ordernumber)) -> find();
                $config = db(\tname::weixin_config) -> where(array('uid'=>2)) -> find();
                $pay = db(\tname::weixin_pay) -> where(array('uid'=>2)) -> find();
                $wxpay = serializeMysql($pay['wxpay'],1);

                $price = $shuju['price'];
                $price = 0.01;
                $dealarr = array(
                    'out_trade_no' 	=> $ordernumber,
                    'openid'		=> $data['openid'],
                    'total_fee' 	=> $price,
                    'appid' 		=> $config['appid'],
                    'partnerkey' 	=> $wxpay['partnerkey'],
                    'mch_id' 		=> $wxpay['mchid'],
                    'body' 			=> '支付',
                    'uid'          => 2,
                    'notify_url' 	=> get_domain().'/wechat.php/Special/order/uid/2',
                );
                $Wxpay = new \wechat\Wxpay();
                $prepay_id = $Wxpay -> getprepayid($dealarr);

                $jsApiParameters = $Wxpay -> getparameters($prepay_id,$dealarr);
                $jsApiParameters = json_decode($jsApiParameters);

                //访问接口
                $result1=phone($orderinfo['name'],$orderinfo['phone'],$orderinfo['people']);//手机认证
                $orderdata['returndata']=serializeMysql($result1);
                $result2=black($orderinfo['name'],$orderinfo['phone'],$orderinfo['people']);//黑名单认证
                if(array_key_exists('data',$result2['result'])){
                    $totalCounts = $result2['result']['data']['totalCounts'];
                    foreach($totalCounts as $key=>&$value){
                        $value['blackType'] = $this->totalCounts[$value['blackType']];
                        $ccc['a'][$value['blackType']]=$value;
                    }
                    $list = $result2['result']['data']['list'];
                    $lists['b']=arr()['b'];
                    foreach($list as $key=>&$value){
                        $value['blackRiskType'] = $this->list[$value['blackRiskType']];
                        $value['blackFactsType'] = $this->list[$value['blackFactsType']];
                        if($value['blackRiskType']!="多头借贷"){
                            $ccc['b'][$value['blackRiskType']]['a']=$value['blackRiskType'];
                            $ccc['b'][$value['blackRiskType']]['b'][]=$value;
                            $lists['b'][$value['blackRiskType']]['b']=$ccc['b'][$value['blackRiskType']]['b'];
                        }else{
                            $ccc['b'][$value['blackFactsType']]['a']=$value['blackFactsType'];
                            $ccc['b'][$value['blackFactsType']]['b'][]=$value;
                            $lists['b'][$value['blackFactsType']]['b']=$ccc['b'][$value['blackFactsType']]['b'];
                        }

                    }
                    $lists['a']=array_merge(arr()['a'],$ccc['a']);
                    $result2['result']['data'] =  $lists ;
                }else{
                    $result2['result']['data']= arr();
                }
                $orderdata['return']=serializeMysql($result2);
                $res = db('shuju_order') -> where(array('ordernumber'=>$orderinfo['ordernumber'])) -> update($orderdata);
                return json(array('ret'=>1,'parameter'=>$jsApiParameters,'id'=>$orderinfo['id'],'free'=>0));
            }
        }else{
            return json(array('ret'=>0,'msg'=>'支付失败'));
        }
    }
    /**
     * 详情
     */
    public function detail(){
        $data = input('post.');

        $order=db('shuju_order')->where(array('id'=>$data['id']))->find();
        $order['phone1']=substr(trim($order['phone']),0,3);
        $order['phone2']=substr(trim($order['phone']),-4);
        $order['people1']=substr(trim($order['people']),0,3);
        $order['people2']=substr(trim($order['people']),-4);
        $order['createtime']=date("Y/m/d",$order['createtime']);
        $order['returndata']=serializeMysql($order['returndata'],1);
        $order['return']=serializeMysql($order['return'],1);
        $rdata = array(
            'order'		=> $order,
        );
        return json($rdata);
    }
    public function details(){
        $order=db('shuju_order')->where(array('uid'=>2,'ispay'=>1,'ishidden'=>0))->order("id asc")->limit(1)->find();

        $order['phone1']=substr(trim($order['phone']),0,3);
        $order['phone2']=substr(trim($order['phone']),-4);
        $order['people1']=substr(trim($order['people']),0,3);
        $order['people2']=substr(trim($order['people']),-4);
        $order['createtime']=date("Y/m/d",$order['createtime']);
        $order['returndata']=serializeMysql($order['returndata'],1);
        $order['return']=serializeMysql($order['return'],1);
        $rdata = array(
            'order'		=> $order,
        );
        return json($rdata);
    }
    /**
     * 记录页
     */
    public function record(){
        $data = input('post.');

        $order=db('shuju_order')->where(array('uid'=>2,'openid'=>$data['openid'],'ispay'=>1,'ishidden'=>0))->order("id desc")->select();
        foreach($order as $k=>&$v){
            //        $v['name1']=substr(trim($v['name']),-1);
            $v['phone1']=substr(trim($v['phone']),0,3);
            $v['phone2']=substr(trim($v['phone']),-4);
            $v['people1']=substr(trim($v['people']),0,3);
            $v['people2']=substr(trim($v['people']),-4);
            $v['createtime']=date("Y-m-d H:i;s",$v['createtime']);
        }
        $rdata = array(
            'order'		=> $order,
        );
        return json($rdata);
    }
   public function is_idcard( $id )
    {
        $id = strtoupper($id);
        $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
        $arr_split = array();
        if(!preg_match($regx, $id))
        {
            return FALSE;
        }
        if(15==strlen($id)) //检查15位
        {
            $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";

            @preg_match($regx, $id, $arr_split);
            //检查生日日期是否正确
            $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
            if(!strtotime($dtm_birth))
            {
                return FALSE;
            } else {
                return TRUE;
            }
        }
        else      //检查18位
        {
            $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
            @preg_match($regx, $id, $arr_split);
            $dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
            if(!strtotime($dtm_birth)) //检查生日日期是否正确
            {
                return FALSE;
            }
            else
            {
                //检验18位身份证的校验码是否正确。
                //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
                $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                $sign = 0;
                for ( $i = 0; $i < 17; $i++ )
                {
                    $b = (int) $id{$i};
                    $w = $arr_int[$i];
                    $sign += $b * $w;
                }
                $n = $sign % 11;
                $val_num = $arr_ch[$n];
                if ($val_num != substr($id,17, 1))
                {
                    return FALSE;
                } //phpfensi.com
                else
                {
                    return TRUE;
                }
            }
        }

    }
}